install.packages('https://cran.r-project.org/src/contrib/Archive/interflex/interflex_1.0.8.tar.gz', 
                 repos=NULL, 
                 type='source')


library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(interflex)
library(gridExtra)
library(readxl)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


paste0(home, 'Code/interflex-kernel-spillover.R') %>%
  source
paste0(home, 'Code/randomization-inference-marginal-effects.R') %>%
  source


permutation.results = paste0(home, 'Data/RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'Data/experimental-analyses.Rds') %>%
  readRDS


dv_floor = paste0(home, 'Data/floor-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         CitizenXProp.Citizen=Citizen*Prop.Citizen, 
         FirmXProp.Firm=Firm*Prop.Firm) %>%
  subset(!is.na(Treatment))
class(dv_floor) = 'data.frame'


tmp = ggplot(data=dv_floor, 
             aes(x=Prop.Citizen, fill=factor(Citizen))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
citizen.floor.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>% 
  mutate(xmin=0.974*(xmin+abs(min(xmin))), 
         xmax=0.974*(xmax+abs(min(xmin))), 
         ymin=ymin/160-1, 
         ymax=ymax/160-1)


tmp = ggplot(data=dv_floor, 
             aes(x=Prop.Firm, fill=factor(Firm))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
firm.floor.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>%
  mutate(xmin=0.974*(xmin+abs(min(xmin))),
         xmax=0.974*(xmax+abs(min(xmin))), 
         ymin=ymin/160-1, 
         ymax=ymax/160-1)


dv_caucus = paste0(home, 'Data/caucus-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         CitizenXProp.Citizen=Citizen*Prop.Citizen, 
         FirmXProp.Firm=Firm*Prop.Firm) %>%
  subset(!is.na(Treatment))
class(dv_caucus) = 'data.frame'


tmp = ggplot(data=dv_caucus, 
             aes(x=Prop.Citizen, fill=factor(Citizen))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
citizen.caucus.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>% 
  mutate(xmin=0.974*(xmin+abs(min(xmin))), 
         xmax=0.974*(xmax+abs(min(xmin))), 
         ymin=ymin/160-1, 
         ymax=ymax/160-1)


tmp = ggplot(data=dv_caucus, 
             aes(x=Prop.Firm, fill=factor(Firm))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
firm.caucus.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>%
  mutate(xmin=0.974*(xmin+abs(min(xmin))),
         xmax=0.974*(xmax+abs(min(xmin))), 
         ymin=ymin/160-1, 
         ymax=ymax/160-1)
rm(tmp)


fig_7_3_top_left = rimer(result='tab3col6', 
                         X='Citizen', 
                         Z='Prop.Citizen', 
                         Label='A) Group caucuses')
fig_7_3_top_right = rimer(result='tab3col4', 
                          X='Citizen', 
                          Z='Prop.Citizen', 
                          Label='B) Floor debates')
fig_7_3_bottom_left = rimer(result='tab3col6', 
                            X='Firm', 
                            Z='Prop.Firm', 
                            Label='C) Group caucuses')
fig_7_3_bottom_right = rimer(result='tab3col4', 
                             X='Firm', 
                             Z='Prop.Firm',
                             Label='D) Floor debates')
fig_7_3 = grid.arrange(fig_7_3_top_left, fig_7_3_top_right, 
                       fig_7_3_bottom_left, fig_7_3_bottom_right, 
                       nrow=2)


ggsave(filename='figure-A07-3.png', 
       plot=fig_7_3, 
       path=paste0(home, 'Figures/'), 
       width=12, 
       height=7, 
       units='in')
ggsave(filename='figure-A07-3.eps', 
       plot=fig_7_3, 
       path=paste0(home, 'Figures/'), 
       width=12, 
       height=7, 
       units='in', 
       device=cairo_ps)
